

#include "defines.h"

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>

#include <avr/io.h>
#include <avr/pgmspace.h>

#include <util/delay.h>
#include <avr/interrupt.h>

//#include "uart.h"

//make firmware.hex
//sudo make install 
//FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

#define TRUE 1
#define FALSE 0

void setup_clock(void) {
    //set to run at 8MHz
  CLKPR = _BV(CLKPCE);
  CLKPR = 0x80;
  CLKPR = 0x00;
 
}

void timer_init(){

  //'fast pwm' mode, 1x prescaler clock
  TCCR0A |= 0x03;
  TCCR0B |= 0x08 | 0x01;
  
  TIMSK |= (1<<TOIE0);
  OCR0A = 255;

  sei();

}

#define PWM_MAX 255;
uint8_t pwm_cnt = 0;
volatile uint8_t pwm_color[3] = {0,0,0};

uint16_t const delayNum = 250;
uint8_t const finalNum = 6;

ISR(TIM0_OVF_vect) {
  
    pwm_cnt += 1;
    
    if (!pwm_cnt) {
      PORTB = 0;
    } else {
      if (pwm_color[0] == pwm_cnt) {
        PORTB |= 0x08;
      }
      if (pwm_color[1] == pwm_cnt) {
        PORTB |= 0x10;
      }
      if (pwm_color[2] == pwm_cnt) {
        PORTB |= 0x02;
      }
    }
    
}

//PINB PINC (SCL LINE)
//;PUT IN WHILE, TO MONITOR THIS LINE, AND WHEN IT GOES FROM HIGH TO LOW, i GUESS, TOGGLE IT TO SOMETHING ELSE

//better explanation: 



int main(void)
{
  //setup_clock();
  timer_init();
  //pin 2 =0x08 = PB3 = red LED;
  //pin 3 =0x10 = PB4 = green LED;
  //pin 6 =0x02 = PB1 = blue LED;
  
  //#define PINTOTEST 0x02
  //DDRB |= PINTOTEST;
  DDRB |= 0x1a;
  
  timer_init(); 
  
  uint8_t i = 0;
  uint8_t tempi = 0;
  uint8_t lastVal = TRUE;
  while (1) {
    //this is the toggle stuff
    //normal functionality.
    
    //if (PINB & (1<<0)) {
    
    uint8_t pinVal = FALSE;
    if(PINB & (1<<0))
    {
      pinVal = TRUE;
    }
    
    //if((pinVal != lastVal) && pinVal){
    if((pinVal != lastVal) ){
      lastVal = pinVal;
      
      if (pinVal) {
        i+= 1;
        if(i>finalNum)
        {
          i=0;
        }
      
      
        //red
        if(i==0){
          pwm_color[0]=1;
          pwm_color[1]=256;
          pwm_color[2]=256;
          
        }
        /**TEMP CODE **/
        //green
        if(i==2){
          pwm_color[0]=256;
          pwm_color[1]=1;
          pwm_color[2]=256;
          
        }
        //blue
        if(i==4){
          pwm_color[0]=256;
          pwm_color[1]=256;
          pwm_color[2]=1;
          
        }
        
        /*
        //yellow
        if(i==6){
          pwm_color[0]=1;
          pwm_color[1]=1;
          pwm_color[2]=256;
          
        }
        
        //purple
        if(i==8){
          pwm_color[0]=1;
          pwm_color[1]=256;
          pwm_color[2]=1;
          
        }
        
        //light blue
        if(i==10){
          pwm_color[0]=256;
          pwm_color[1]=1;
          pwm_color[2]=1;
          
        }
        
        //white
        if(i==12){
          pwm_color[0]=1;
          pwm_color[1]=1;
          pwm_color[2]=1;
          
        }
        */
        /**TEMP CODE **/
        /*
        //orange
        
        if(i==2){
          pwm_color[0]=1;
          pwm_color[1]=90;
          pwm_color[2]=255;
          
        }
        //yellow
        if(i==4){
          pwm_color[0]=1;
          pwm_color[1]=1;
          pwm_color[2]=256;
          
        }
        //green
        if(i==6){
          pwm_color[0]=256;
          pwm_color[1]=1;
          pwm_color[2]=256;
          
        }
        //blue
        if(i==8){
          pwm_color[0]=256;
          pwm_color[1]=256;
          pwm_color[2]=1;
          
          
        }
        //pink
        if(i==10){
          pwm_color[0]=1;
          pwm_color[1]=63;
          pwm_color[2]=52;       
          
        }
        //purple
        if(i==12){
          pwm_color[0]=128;
          pwm_color[1]=256;
          pwm_color[2]=128;
          
          
        }
        //white
        if(i==14){
          pwm_color[0]=1;
          pwm_color[1]=1;
          pwm_color[2]=1;
          
          
        }
        
        if(i==16){
          pwm_color[0]=256;
          pwm_color[1]=256;
          pwm_color[2]=256;
          
          
        }
        */
        
        if(i==finalNum){
          pwm_color[0]=256;
          pwm_color[1]=256;
          pwm_color[2]=256;
          
          
        }
        
        //if(tempi != i)
        //{
        //  _delay_ms(1000);
        //  tempi = i;
        //} 
        
        
        
        //_delay_ms(150); 
        _delay_ms(delayNum);
        
      }
    }
    //if toggled
   
    
    //_delay_ms(5);
  }

  
  return 0;
}
